package cn.iocoder.yudao.im.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.iocoder.yudao.im.domain.vo.ModelParameter;
import cn.iocoder.yudao.im.domain.vo.ModelResponse;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author tt
 * @since 2024-06-26
 */
@Service
public class AiUtil {


    @Value("${ai.url}")
    private String aiUrl;

    @Value("${ai.model}")
    private String aiModel;

    /**
     * 发起聊天
     *
     * @param modelParameter 给入内容
     * @return 内容
     */
    public ModelResponse chat(@RequestBody ModelParameter modelParameter) {
        modelParameter.setModel(aiModel);
        modelParameter.setStream(false);
        HttpRequest post = HttpUtil.createPost(aiUrl);
        String jsonString = JSON.toJSONString(modelParameter);
        System.err.println("发起请求：" + jsonString);
        post.body(jsonString);
        String body = post.execute().body();
        System.err.println("接收响应：" + body);
        return JSON.parseObject(body, ModelResponse.class);
    }
}
